package controller;

import java.io.IOException;
import java.sql.Connection;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import model.BookRespository;
import model.Repository;
import myBeans.BookInfo;
import myBeans.UserInfo;

/**
 * Servlet implementation class AddBookServlet
 */
@WebServlet("/AddBookServlet.do")
public class AddBookServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
	
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddBookServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		processRequest(request, response);
	}
	protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		
		Connection conn = (Connection) getServletContext().getAttribute("connection");

		BookInfo book = new BookInfo();
		
		book.setType(request.getParameter("bookType"));
		book.setIsbn(request.getParameter("isbn"));
		book.setOldISBN(request.getParameter("oldISBN"));
		book.setBookNameEng(request.getParameter("bookNameEN"));
		book.setBookNameThai(request.getParameter("bookNameTH"));		
		book.setAuthor(request.getParameter("author"));	
		book.setPublisher(request.getParameter("publisher"));	
		book.setPublishNo(Integer.parseInt(request.getParameter("publishNo")));	
		book.setTranslator(request.getParameter("translator"));
		book.setPrice(Integer.parseInt(request.getParameter("price")));
		book.setTotalPage(Integer.parseInt(request.getParameter("totalPage")));
				
		boolean addBookSuccess = new BookRespository(conn).AddBook(book);
		if(addBookSuccess){
			response.sendRedirect("AdminAddBook_success.jsp?isbn=" + book.getIsbn());
		}
		else
		{
			response.sendRedirect("AdminAddBook_Failure.jsp");
		}
	}

}
